diary on;
%% Berkakis antonis aem 808 2o etos
%% proto meros ergasia 4

% askisi 1

a=[2 1 1]';
b=[-1 1 1]';

a'*b

%den einai monadiko to dianysma gt ena allo pou yparxei einai to b = [1 2-4]';

pause;
% askisi 2

a = [2 1 1]'; 
b = [1 2 -4]';
c = cross(a,b); 
figure(1); 
clf;
drawVector([a b c], 'r.', {'a','b','c'});
view(-160, 7)

%askisi 3

 A = [a b c];
 X = sampleUnitSphere(3,200);
 AX = A*X;
 figure(1); clf; 
 hold on;
 plot3( X(1,:), X(2,:), X(3,:),'b.');
 view(-60,10)
 plot3(AX(1,:),AX(2,:),AX(3,:),'g.');
 drawVector(A, 'r.') 
 hold off; 
 axis square equal; 
 grid on;
    % oi  stiles toy pinaka einai kathetes metaksi toys...
% askisi 4
disp('asksi 4');
 B = [a b c]
 C = diag([1/norm(a) 1/norm(b) 1/norm(c)]) 
 A = B*C
 
 X = sampleUnitSphere(3,200);
 AX = A*X; 
 figure(1);
 clf;
 hold on; 
 plot3( X(1,:),X(2,:), X(3,:),'b.');
 view(-60,10) 
 plot3(AX(1,:),AX(2,:),AX(3,:),'g.');
 drawVector(A, 'r.') 
 hold off; 
 axis square equal; 
 grid on;
 
 A=A*B;
 
 AX = A*X; 
 figure(1);
 clf;
 hold on; 
 plot3( X(1,:),X(2,:), X(3,:),'b.');
 view(-60,10) 
 plot3(AX(1,:),AX(2,:),AX(3,:),'g.');
 drawVector(A, 'r.') 
 hold off; 
 axis square equal; 
 grid on; 
 
 A=A*A;
  AX = A*X; 
 figure(1);
 clf;
 hold on; 
 plot3( X(1,:),X(2,:), X(3,:),'b.');
 view(-60,10) 
 plot3(AX(1,:),AX(2,:),AX(3,:),'g.');
 drawVector(A, 'r.') 
 hold off; 
 axis square equal; 
 grid on;
 
 
 
 
 A=A*A;
  AX = A*X; 
 figure(1);
 clf;
 hold on; 
 plot3( X(1,:),X(2,:), X(3,:),'b.');
 view(-60,10) 
 plot3(AX(1,:),AX(2,:),AX(3,:),'g.');
 drawVector(A, 'r.') 
 hold off; 
 axis square equal; 
 grid on;
 
 
 % askisi 5
 disp('askisi 5');
 pause;
 b = [1 -1;
     1 1];
 % ta dianysmata einai hdh kanonikopoihmena
 figure(1);
 clf;
 hold on;
hV = drawVector(b, {'a', 'b', 'c'});

hold off;


%% askisi 1
clear all;
A = [-2 3; -3 1 ];
B = [1 3; 1 1]
C = reshape(1:4,2,2)
 
 %% askisi 2
 
 %% askisi 3
 
 %% askisi 4
 
 clear all;
 A = [1 3 -2; 8 0 1; 2 2 6 ];
 rank (A) % i taksi toy pinaka einai 3...
 
 B = reshape(1:9,3,3);
 rank(B) % i taksi toy pinaka einai 2
 
 
 %% askisi 5
 
 
 %% provoles
 % askisi 1
 
 A = [1 0; 0 1; 1 0];
 % i provolip ston xoro stilon einai 1 , 0
 B = [1 2; 0 1; 1 0];
 
 a = [1 2 3]';
 
 
 % askisi 2
 
 P_oblique_A = A*A'*inv([A a])
 
 P_oblique_B = B*B'*inv([B a])
 
 
 
 
 
 
  %% antistrofos pinakas
  
  
  % askisi 1
  %i proti stili toy antistrofou eixei stoixeia [0.5 0.5]' apo to sxima
  % eno i deyteri stili ta stoixeia einai [0.25 -0.25]'
  
  
  % askisi 2 
  
  e1 = [1;0]; e2 = [0;1];
  figure(1);
  clf;
  hold on; 
  drawVector(A, {'a','b'});
  drawVector([e1 e2], 'gs', {'e_1', 'e_2'})
  hold off;
  
  
  A = [ 1 1; -2 2 ];
  
  inv(A)
  
  
  invA = inv(A) % Compute inverse of A 
  P1 = A*diag(invA(:,1)); 
  % Find the *oblique projections* of e_1 
  P2 = A*diag(invA(:,2)); 
  % and of e_2 onto the columns of A 
  figure(1);
  hold on; 
  % Plot the projections 
  drawVector(P1, 'rd') 
  drawVector(P2, 'ms') 
  drawLine([e1 P1(:,1)]); 
  % Indicate the projection lines for e_1 
  drawLine([e1 P1(:,2)]); 
  drawLine([e2 P2(:,1)]); 
  % and for e_2 
  drawLine([e2 P2(:,2)]);
  
  hold off;
  
  
  %% paragontopoihsh LU
  
  
  %askisi 1
  
  A = [ 2 4; 3 6 ];
  b = [4 7]';
  
  
  figure(3); clf; hold on;
  drawPlane(A(1,:), b(1), 'b'); 
  drawPlane(A(2,:), b(2), 'g'); 
  hold off 
  title('Ax=b in the row space', 'FontSize', 14);
  
  
  
  x = A\b; 
  figure(3);
  clf; 
  hold on; 
  drawPlane(A(1,:), b(1), 'b'); 
  drawPlane(A(2,:), b(2), 'g');
  hold off 
  title('Ax=b in the row space', 'FontSize', 14); 
  figure(3); hold on; 
  plot(x(1), x(2), 'rx', 'MarkerSize', 16, 'LineWidth', 2)
  hold off;
  
  
  % askisi 2
  
  A = [3 12 1; 12 0 2; 0 2 3];
  b = [2.36 5.26 2.77]';
  x = A\b; 
  Pb = A*diag(x); 
  figure(1);clf;hold on; 
  drawVector(A, {'a_1','a_2','a_3'}); 
  drawVector(b, 'rs', {'b'}); 
  drawLine([b Pb(:,1)]);
  hold off;
  title('Ax=b in the column space', 'FontSize', 14);
  
  figure(3); clf; hold on;
drawPlane(A(1,:), b(1), 'b'); 
drawPlane(A(2,:), b(2), 'g'); 
drawPlane(A(3,:), b(3), 'r'); 
plot3(x(1),x(2),x(3), 'mx', 'LineWidth', 4, 'MarkerSize', 15)
hold off
title('Ax=b in the row space', 'FontSize', 14);
  
  
  
  
  
  %askisi 3
  
A = [ 4 1 ; 0 3.5 ]; b = [2 -3]'; figure(3); clf; hold on; drawPlane(A(1,:), b(1), 'b'); drawPlane(A(2,:), b(2), 'g'); hold off title('Ax=b in the row space', 'FontSize', 14); figure(3); hold on;
plot(x(1), x(2), 'rx', 'MarkerSize', 16, 'LineWidth', 2); hold off;
A = [ 4 1 ; 0 3.5 ];
b = [2 -3]';
x = A\b;
figure(2); clf; hold on; 
drawVector(A, {'a_1', 'a_2'}) 
drawVector(b, 'rs', {'b'}); Pb = A*diag(x); 
drawVector(Pb, 'g+', {'x_{11}\cdot a_1', 'x_{21}\cdot a_2'}); 
drawLine([b Pb(:,1)]); 
drawLine([b Pb(:,2)]);
hold off; 
title('Ax=b in the column space', 'FontSize', 14);  
  
  
  
% askisi 4

U = [ 2 1 1 0; 0 1 1 1; 0 0 2 2; 0 0 0 2 ]; 
I=eye(4); 
for j=1:4 
    b=I(1:4,j); 
    x(1:4,j)=backSub(U,b); 
end 
V=U; 
display(x); display(V);
  
  
  
  % askisi 6
  
  A= [ 2 1 1 0; 4 3 3 1; 8 7 9 5; 6 7 9 8 ]; 
  [L, U] = slu(A,1);
b = (2:2:8)';  
  
  % askisi 7
  
  A= [ 2 1 1 0; 4 3 3 1; 8 7 9 5; 6 7 9 8 ]; 
  [L, U] = slu(A,1);
  %[L, U] = lu(A,1);
  
diary off;  
  
  
  
  
  
  
  
  